package com.violet.lb.utils;


import com.alibaba.dubbo.common.Constants;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.violet.common.constant.universal.GlobalEnums.Symbol.BACKSLASH;
import static com.violet.common.constant.universal.GlobalEnums.Symbol.QUESTION_MARK_EN;


/**
 * 类说明: url参数截取
 *
 * @author wqf
 * @date 2022/1/14 16:50
 */
public class HttpParamUtil {

    private HttpParamUtil() {
    }


    /**
     * 方法描述: 通过uri获取参数map
     *
     * @param uri uri
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author wqf
     * @date 2022/10/26 16:41
     */
    public static Map<String, String> getQueryMap(URI uri) {
        if (Objects.nonNull(uri)) {
            return getParameter(uri.getQuery());
        }
        return Collections.emptyMap();
    }

    /**
     * 方法描述: 获得url中参数
     *
     * @param url 请求地址
     * @return java.util.Map
     * @author wqf
     * @date 2022/1/14 16:42
     */
    public static Map<String, String> getParameter(String url) {
        try {
            url = URLDecoder.decode(url, StandardCharsets.UTF_8);
            if (url.contains(QUESTION_MARK_EN.getValue())) {
                return parameterInterception(url.split(BACKSLASH.getValue() + QUESTION_MARK_EN.getValue())[1]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 方法描述: 获取request中参数
     *
     * @param request 页面请求
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author wqf
     * @date 2022/1/14 16:42
     */
    public static Map<String, String> getRequestParameters(HttpServletRequest request) {
        // 网址中的参数
        String urlParameter = request.getQueryString();
        if (urlParameter != null && !"".equals(urlParameter)) {
            urlParameter = URLDecoder.decode(urlParameter, StandardCharsets.UTF_8);
        } else {
            urlParameter = "";
        }
        return parameterInterception(urlParameter);
    }


    /**
     * 方法描述: 将url中的参数分割出来，放到map中
     *
     * @param parameters 参数字符串
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author wqf
     * @date 2022/1/14 16:44
     */
    private static Map<String, String> parameterInterception(String parameters) {
        Map<String, String> map = new HashMap<>();
        if (parameters != null && !"".equals(parameters.trim())) {
            String[] arr = parameters.split("&");
            for (String s : arr) {
                String key = s.substring(0, s.indexOf("="));
                String value = s.substring(s.indexOf("=") + 1);
                map.put(key, value);
            }
            return map;
        }
        return Collections.emptyMap();
    }

    /**
     * 方法描述: 获取主域名，即URL头
     *
     * @param url 请求地址
     * @return java.lang.String
     * @author wqf
     * @date 2022/1/23 17:34
     */
    public static String getDomainHost(String url) {
        String pattern = "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/)";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(url);
        if (m.find()) {
            //匹配结果
            String domain = m.group();
            domain = domain.replace("https", "http");
            return domain;
        }
        return null;
    }
}